﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.Script.Serialization;


/// <summary>
/// Summary description for ClassName
/// </summary>
public class BankPartnerManager
{
    public BankPartnerManager()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    
    public static int CreateBankPartner(BankPartner b){
        
        string sql = @"INSERT INTO BankPartnerProfile(
            CardExpiry,
            CardLength,
            CardPrefix,
            AuthContactPerson,
            AuthEmail,
            Active,
            DateCreated,
            DateUpdated,
            Code,
            Name)
            VALUES(@0,@1,@2,@3,@4,@5,@6,@7,@8,@9)";
           List<object> param = new List<object>();
           
            param.Add(b.CardExpiry);
            param.Add(b.CardLength);
            param.Add(b.CardPrefix);
            param.Add(b.AuthContactPerson);
            param.Add(b.AuthEmail);
            param.Add(b.Active);
            param.Add(b.DateCreated);
            param.Add(b.DateUpdated);
            param.Add(b.Code);
            param.Add(b.Name);
          var result = DBManager.Execute(sql,param.ToArray());
          return result;
          
    }
    
    
        public static dynamic CheckExistBP(string code, string name){

         string sql = "SELECT * FROM BankPartnerProfile WHERE Code=@0 AND Name=@1";
        object[] param = new object[2];
            param[0]= code;
            param[1]= name;
            
        var query = DBManager.Query(sql,param);
            if(query.Count>0){
                return query[0];
            }else{
                return null;    
            }
         

    }
        public static IList<dynamic> SearchBP(BankPartner bp){
            try{
                var sql = string.Format("SELECT * FROM BankPartnerProfile WHERE Active={0}",bp.Active);
                Dictionary<string,string> param = new Dictionary<string,string>();
                StringBuilder sb = new StringBuilder();
                sb.Append(sql);
                
                string cond=" AND ";
                
                if(bp.Code!=""){
                    sb.Append(cond).Append(string.Format("Code LIKE '{0}%'",bp.Code));     
                }
                
                if(bp.Name!=""){
                    sb.Append(cond).Append(string.Format("Name LIKE '{0}%'",bp.Name));     
            
                }
                
                var query = DBManager.Query(sb.ToString(),null);
                return query;
               
                
            }catch(Exception e){
                throw e;
            }
        }
        
        public static dynamic GetBP(int id){
            try{
                var sql = string.Format("SELECT * FROM BankPartnerProfile WHERE ID={0}",id);
                var bp = DBManager.Query(sql,null);
                
                return bp[0];
                
            }catch(Exception e){
                throw e;
            }
        }
        
        public static int UpdateBP(BankPartner b){
            try{
                  string sql = @"UPDATE BankPartnerProfile 
                    SET CardExpiry=@0,
                    CardLength=@1,
                    CardPrefix=@2,
                    AuthContactPerson=@3,
                    AuthEmail=@4,
                    Active=@5,
                    DateUpdated=@6,
                    Code=@7,
                    Name=@8
                    WHERE ID=@9";
                List<object> param = new List<object>();
                
                    param.Add(b.CardExpiry);
                    param.Add(b.CardLength);
                    param.Add(b.CardPrefix);
                    param.Add(b.AuthContactPerson);
                    param.Add(b.AuthEmail);
                    param.Add(b.Active);
                    param.Add(DateTime.Now);
                    param.Add(b.Code);
                    param.Add(b.Name);
                    param.Add(b.ID);
                var result = DBManager.Execute(sql,param.ToArray());
                return result;
            }catch(Exception e){
                throw e;
            }
        }
    

}
